

jQuery(function ($) {
    var user = JSON.parse(localStorage.getItem('user_info'));
    var goods_id = window.location.href.split('-')[1].split('.')[0];
    if (user) {
        var id = user.id;
    }

    //导航栏滚动到顶部的时候置顶
    var goods_OffTop = $(".good_info").offset().top; //获取到距离顶部的垂直距离
    var scTop = 0; //初始化垂直滚动的距离
    $(document).scroll(function () {
        scTop = $(this).scrollTop(); //获取到滚动条拉动的距离
        ////查看滚动时，垂直方向上，滚动条滚动的距离
        if (scTop >= goods_OffTop) {
            //核心部分：当滚动条拉动的距离大于等于导航栏距离顶部的距离时，添加指定的样式
            $(".good_info").addClass("fixed-top");
        } else {
            $(".good_info").removeClass("fixed-top");
            $(".good_info li").eq(0).addClass('active');
        }
    });
    //分页组件
    //字画晒单/评论
    var assess_param = {
        " count": 0,
        "[]": {
            "Comment": {
                "holder_id": goods_id,
                "Comment": {
                    "parent_id@": "/id",
                    "yc_reply_flag": 1
                }
            },

            "YcUser": {
                "id@": "/Comment/user_id"
            },
            "Goods": {
                "id": "1"
            },
            "CommentImg[]": {
                "CommentImg": {
                    "comment_id@": "[]/Comment/id"
                }
            },
            "Like": {
                "type": 3,
                "user_id": id,
                "holder_id@": "/Comment/id"

                //主评论列表
            }
        }
    };
    var assess_data;
    if (id) {
        send_ajax('post', '/get', assess_param, function callback(data) {
            if (data.code == 200 && data["[]"]) {
                assess_data = data["[]"];
            }
            if (assess_data && assess_data.length !== 0) {
                //一页显示条数
                var generateList = function generateList(page, data) {
                    //接受评论列表的html
                    var assess_html = "";
                    var start = (page - 1) * pageSize;
                    var end = (page - 1) * pageSize + pageSize;
                    if (end > data.length) {
                        end = data.length;
                    }
                    var arr = []; //图片的数组;
                    var zanArr = []; //带赞的数组;
                    var yc_Reply = [];
                    for (var i = start; i < end; i++) {

                        // //点赞个数函数调用
                        // thumbs_up_number(assess_data[i].Comment.id);

                        // //点赞的状态
                        // is_thumbs_up(assess_data[i].Comment.id)
                        // 子评论个数函数调用
                        sub_commentary_number(assess_data[i].Comment.id);
                        //有点赞的
                        if (assess_data[i].Like) {
                            zanArr.push(assess_data[i].Like);
                        }
                        //有晒得图片
                        if (assess_data[i]["CommentImg[]"]) {
                            for (var j = 0; j < assess_data[i]['CommentImg[]'].length; j++) {
                                arr.push(assess_data[i]['CommentImg[]'][j]);
                            }
                        }
                        if (assess_data[i].Comment.Comment) {
                            yc_Reply.push(assess_data[i].Comment.Comment);
                        }
                        if (assess_data[i].Goods) {
                            assess_html += "<li class=\"drying_list_list_item  clearfix\" data-parent-id=" + assess_data[i].Comment.id + " >\n                                            <div class=\"list_item_left \">\n                                                <div class=\"list_item_left_pic\">\n                                                     <img src=\"" + (assess_data[i].YcUser ? isImgHttp(assess_data[i].YcUser.portrait) : "") + "\" alt=\"\">\n                                                </div>\n                                                 <p>" + (assess_data[i].YcUser ? assess_data[i].YcUser.login_name : "") + '</p>\n                                            </div>\n                                            <div class="list_item_content">\n                                                <div class="shop_name">\u5546\u54C1\u540D\u79F0\uFF1A' + assess_data[i].Goods.name + "<span> " + assess_data[i].Comment.create_time.replace('.0', '') + "</span></div>\n                                                <div class=\"shop_describe\">" + assess_data[i].Comment.content + "</div>\n                                                <ul class=\"slide_show clearfix\" data_comment_id=\"" + assess_data[i].Comment.id + "\">\n                                                \n                                                </ul>\n                                                <ul class=\"slide_show_pic\">\n                                                \n                                                </ul>\n                                                <div class=\"list_item_content_ico\">\n                                                    <div data_comment_id=" + assess_data[i].Comment.id + " class=\"thumbs_up points_number\" >\n                                                        <span>\n                                                            <i class=\"thumbs_up_btn thumbs_up_ico\"></i>\n                                                            <span class=\"thumbs_up_number\">" + assess_data[i].Comment.likes_v + '</span>\n                                                        </span>\n                                                        <span style="margin-left:30px;" class="comment">\n                                                            <i class="glyphicon_comment"></i>\n                                                            <span class="comment_number">3</span>\n                                                        </span>\n                                                    </div>\n                                                <div class="child_assess">\n                                                    <div class="reply_textarea"\t>\n                                                        <div class="wrap_textarea">\n                                                            <textarea class="reply_input" placeholder="\u56DE\u590D\uFF1A"></textarea>\n                                                        </div>\n                                                        <div class="submit_box">\n                                                            <a class="reply_submit">\u63D0\u4EA4</a>\n                                                        </div>\n                                                    </div>\n                                                    <ul class="children">\n                                                        \n                                                    </ul>\n                                                    </div>  \n                                                </div>\n                                                <div class="yc_reply" data_comment_id="' + assess_data[i].Comment.id + "\">\n                                                   \n                                                </div>\n                                            </div>\n                                        </li>";
                        }
                    }
                    $('.drying_list_list').html(assess_html);
                    var slide_show = $('.slide_show'); //小图的盒子
                    var slide_show_pic = $('.slide_show_pic'); //放大图的盒子
                    for (var n = 0; n < slide_show.length; n++) {
                        for (var m = 0; m < arr.length; m++) {
                            if ($(slide_show[n]).attr('data_comment_id') == arr[m].comment_id) {
                                $(slide_show[n]).append(pic_html(arr[m]));
                                $(slide_show_pic[n]).append(pic_html(arr[m]));
                            }
                        }
                    };

                    //有点赞数据的图标变色函数调用
                    is_thumbs_up(zanArr);

                    //带畜牧回复的点赞函数调用
                    ycRely(yc_Reply);

                    //图片的模板
                    function pic_html(data) {
                        var comment_pic_html = "";
                        return comment_pic_html += " <li class=\"" + data.comment_id + "\"><a><img src=\"" + isImgHttp(data.img) + "\" alt=\"\"></a></li> ";
                    }
                    //点赞
                    thums_up();

                    //评论列表图片切换
                    $(".slide_show li").click(function () {
                        $('.slide_show img').removeClass('border');
                        $(this).find('img').addClass('border');
                        var index = $(this).index();
                        //装大图的列表
                        var list = $(this).parents('.drying_list_list_item').find('.slide_show_pic');
                        list.addClass('open');
                        list.find('li').eq(index).addClass('open').siblings().removeClass('open');
                        list.find('li').click(function () {
                            list.removeClass('open');
                            $('.slide_show img').removeClass('border');
                        });
                    });

                    //点击获取子评论列表
                    $('.comment').click(function () {
                        //装子评论的盒子
                        var comment_list = $(this).parents('.list_item_content_ico').find('.child_assess');
                        //子评论盒子的列表
                        var child_comment_list = $(this).parents('.list_item_content_ico').find('.children');
                        var parent_id = $(this).parent().attr('data_comment_id');
                        var comment_param = {
                            "[]": {
                                "Comment": {
                                    "parent_id": parent_id,
                                    "yc_reply_flag": 0
                                },
                                "count": 0,
                                "YcUser": {
                                    "id@": "/Comment/user_id"
                                },
                                "Like": {
                                    "holder_id@": "/Comment/id"
                                }
                            }
                        };
                        if (comment_list.hasClass('open')) {
                            comment_list.removeClass('open');
                        } else {
                            $('.child_assess').removeClass('open');
                            comment_list.addClass('open');
                            updata_replay(this, comment_param);
                        }
                    });
                };
                $(document).on('click', '.reply_submit', function () {
                    if (!id) {
                        alert('您还么没有登录，登录后才可评价！');return;
                    }
                    var parent_id = $(this).parents('.drying_list_list_item').attr('data-parent-id');
                    var that = $(this);
                    var text = $(this).parents('.reply_textarea').find('.reply_input').val();

                    if (!text) {
                        alert('请输入评价的内容！');
                        return;
                    }
                    var param = {
                        content: text,
                        type: 0,
                        user_id: id,
                        holder_id: parent_id
                    };
                    var comment_param = {
                        "[]": {
                            "Comment": {
                                "parent_id": parent_id,
                                "yc_reply_flag": 0
                            },
                            "count": 0,
                            "YcUser": {
                                "id@": "/Comment/user_id"
                            },
                            "Like": {
                                "holder_id@": "/Comment/id"
                            }
                        }
                    };

                    if (text != '' && text != 'undefind') {
                        send_ajax('post', '/api/comment/submit', param, function callback(data) {
                            if (data.code == 200) {
                                that.parents('.reply_textarea').find('.reply_input').val("");
                                updata_replay(that, comment_param);
                            }
                        });
                    }
                });

                //分页按钮
                var pageSize = 5;
                $('#pagination_b').twbsPagination({
                    totalPages: assess_data.length / 5,
                    visiblePages: 5,
                    first: "首页",
                    last: "末页",
                    prev: '&lt;',
                    next: '&gt;',
                    onPageClick: function onPageClick(event, page) {
                        generateList(page, assess_data);
                    }
                });
            }
        });
    };
    //子评论列表     
    function updata_replay(that, comment_param) {
        //装子评论的盒子
        var comment_list = $(that).parents('.list_item_content_ico').find('.child_assess');
        //子评论盒子的列表
        var child_comment_list = $(that).parents('.list_item_content_ico').find('.children');

        var comment_html = "";
        var click_zan = [];
        send_ajax('post', '/get', comment_param, function callback(data) {
            if (data.code == 200) {
                var comment = data["[]"];
            }
            if (comment !== undefined) {
                for (var n = 0; n < comment.length; n++) {

                    // //点赞个数函数调用
                    // thumbs_up_number(comment[n].Comment.id);
                    if (comment[n].Like) {
                        click_zan.push(comment[n].Like);
                    }
                    if (comment[n].YcUser) {
                        comment_html += "<li data_comment_id=" + comment[n].Comment.id + "  class=\"points_number\">\n                                            <div class=\"comment_message\">\n                                                <span>" + comment[n].YcUser.login_name + ":</span>\n                                                <span class=\"comment_content\"> " + comment[n].Comment.content + "</span>\n                                            </div>\n                                            <div class=\"comment_thums_up thumbs_up\" data_comment_id=" + comment[n].Comment.id + ">\n                                                <span>" + comment[n].Comment.create_time.replace('.0', "") + ("</span>\n                                                    <span class=\"last\"><i class=\"thumbs_up_btn thumbs_up_ico\"></i><span class=\"comment_number thumbs_up_number\">" + comment[n].Comment.likes_v + "</span></span>\n                                            </div>\n                                        </li>");
                    } else {
                        comment_html += "<li data_comment_id=" + comment[n].Comment.id + " class=\"points_number\">\n                                        <div class=\"comment_message\">\n                                            <span>" + comment[n].Comment.name_v + ":</span>\n                                            <span> " + comment[n].Comment.content + "</span>\n                                        </div>\n                                        <div class=\"comment_thums_up thumbs_up\" data_comment_id=" + comment[n].Comment.id + ">\n                                            <span>" + comment[n].Comment.create_time.replace('.0', "") + "</span>\n                                            <span class=\"last\"><i class=\"thumbs_up_btn thumbs_up_ico\"></i><span class=\"comment_number thumbs_up_number\">0</span></span>\n                                        </div>\n                                    </li>";
                    }
                }
                child_comment_list.html(comment_html);

                thums_up();
                if (click_zan.length !== 0) {
                    is_thumbs_up(click_zan);
                }
            } else {
                comment_list.addClass('open');
            }
        });
    };
    //有畜牧回复的
    function ycRely(yc_replay) {
        var html = '';
        var replay = $('.yc_reply');
        for (var r = 0; r < yc_replay.length; r++) {
            for (var h = 0; h < replay.length; h++) {
                if (yc_replay[r].parent_id == $(replay[h]).attr('data_comment_id')) {
                    $(replay[h]).html(html += '\n                        \u6613\u4ECE\u56DE\u590D\uFF1A<br/>' + yc_replay[r].content + "\n                        ");
                }
            }
        }
    };
    // 点赞
    function thums_up() {
        $('.thumbs_up_btn').click(function () {
            if (!id) {
                alert('您还没有登录，登录后才可操作。');
            }
            if (!$(this).hasClass('thumbs_up_ico_color')) {
                var _this = this;
                var comment_id = $(this).parents('.points_number').attr('data_comment_id');
                var user = JSON.parse(localStorage.getItem('user_info')) || {};
                var id = user.id;
                var param = {
                    "Like": {
                        "holder_id": comment_id,
                        "user_id": id,
                        "type": 3
                    },
                    "tag": "Like"
                };
                send_ajax('post', '/post', param, function callback(data) {
                    if (data.code == 200) {
                        var number = parseFloat($(_this).next().html());
                        $(_this).next().html(number + 1);
                    }
                });
                $(this).removeClass('thumbs_up_ico').addClass('thumbs_up_ico_color');
            }
        });
    };
    //有点赞数据的图标变色
    function is_thumbs_up(zanArr) {
        var yc_zan = $('.thumbs_up');
        var user = JSON.parse(localStorage.getItem('user_info'));
        for (var r = 0; r < zanArr.length; r++) {
            for (var h = 0; h < yc_zan.length; h++) {
                if (zanArr[r].holder_id == $(yc_zan[h]).attr('data_comment_id') && zanArr[r].user_id == user.id) {
                    $(yc_zan[h]).find('.thumbs_up_btn').removeClass('thumbs_up_ico').addClass('thumbs_up_ico_color');
                }
            }
        }
    };

    // //点赞的个数/需要优化
    function thumbs_up_number(id) {
        var param = {
            "UserCommentLike": {
                "comment_id": id
            }
        };
        send_ajax('post', '/head', param, function callback(data) {
            if (data.code == 200) {

                data.UserCommentLike.comment_id = id;
            }
            var thumbs_up_box = $('.points_number');
            for (var i = 0; i < thumbs_up_box.length; i++) {
                if ($(thumbs_up_box[i]).attr('data_comment_id') == data.UserCommentLike.comment_id) {
                    var number = parseInt($(thumbs_up_box[i]).find('.thumbs_up_number').text());
                    $(thumbs_up_box[i]).find('.thumbs_up_number').text(number + data.UserCommentLike.count);
                }
            }
        });
    };
    //子评论的条数
    function sub_commentary_number(id) {
        var param = {
            "Comment": {
                "parent_id": id
            }
        };
        send_ajax('post', '/head', param, function callback(data) {
            if (data.code == 200) {
                data.Comment.comment_id = id;
            }
            var thumbs_up_box = $('.thumbs_up');
            for (var i = 0; i < thumbs_up_box.length; i++) {
                if ($(thumbs_up_box[i]).attr('data_comment_id') == data.Comment.comment_id) {
                    $(thumbs_up_box[i]).find('.comment_number').text(data.Comment.count);
                }
            }
        });
    };
    //获取当前时间
    function CurentTime() {
        var now = new Date();
        var year = now.getFullYear(); //年
        var month = now.getMonth() + 1; //月
        var day = now.getDate(); //日
        var hh = now.getHours(); //时
        var mm = now.getMinutes(); //分
        var ss = now.getSeconds(); //秒

        var clock = year + "-";

        if (month < 10) clock += "0";

        clock += month + "-";

        if (day < 10) clock += "0";

        clock += day + " ";

        if (hh < 10) clock += "0";

        clock += hh + ":";
        if (mm < 10) clock += '0';
        clock += mm + ":";

        if (ss < 10) clock += '0';
        clock += ss;
        return clock;
    };
    //问题咨询
    var data_param = {
        "[]": {
            "GoodsAsk": {
                "goods_id": goods_id
            },
            "GoodsAskReply": {
                "ask_id@": "/GoodsAsk/id"
            },
            "YcUser": {
                "id@": "/GoodsAsk/user_id"
                //问题咨询
            }
        }
    };
    if (id) {
        send_ajax('post', '/get', data_param, function callback(data) {
            var ask_list_html = "";
            if (data.code == 200 && data["[]"]) {
                var data = data["[]"];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].GoodsAskReply) {
                        ask_list_html += '<li class="question_consultation_item">\n                                            <div class="question_consultation_item_title">\n                                                <span>\u54A8\u8BE2\u5B57\u753B\uFF1A\u859B\u6CE2\u6700\u65B0\u529B\u4F5C\u7261\u4E39\u4E5D\u9C7C\u56FE\u300A\u5B09\u6C34\u300B</span>\n                                            </div>\n                                            <div class="clearfix question_consultation_item_center">\n                                                <div class="ask">\n                                                    \u95EE\n                                                </div>\n                                                <div class="ask_question">\n                                                    <strong>' + data[i].GoodsAsk.content + "</strong>\n                                                </div>\n                                                <div class=\"ask_time\">\n                                                    <span>" + data[i].YcUser.login_name + "</span>\n                                                    <span>" + data[i].GoodsAsk.create_time.replace('.0', "") + '</span>\n                                                </div>\n                                            </div>\n                                            <div class="clearfix">\n                                                <div class="ask answer">\n                                                    \u7B54\n                                                </div>\n                                                <div class="answer_question">\n                                                    <p>\n                                                        \u4F60\u597D\uFF0C\u8FD9\u662F\u859B\u6CE2\u8001\u5E08\u7684\u65B0\u54C1\u82B1\u9E1F\u753B\uFF0C\u73B0\u5728\u6CE8\u518C\u4F1A\u5458\u5C31\u53EF\u4EE5\u4EAB\u53D7\u4E5D\u6298\u4F18\u60E0\uFF0C\u4E5F\u53EF\u4EE5\u76F4\u63A5\u52A0\u5BA2\u670D\u5FAE\u4FE1\uFF1A\n                                                        15910507011\u611F\u8C22\u60A8\u5BF9\u6613\u4ECE\u7F51\u7684\u652F\u6301\uFF01\u795D\u60A8\u8D2D\u7269\u6109\u5FEB\uFF01\n                                                    </p>\n                                                </div>\n                                            </div>\n                                        </li>';
                    } else {
                        ask_list_html += '<li class="question_consultation_item">\n                                            <div class="question_consultation_item_title">\n                                                <span>\u54A8\u8BE2\u5B57\u753B\uFF1A\u859B\u6CE2\u6700\u65B0\u529B\u4F5C\u7261\u4E39\u4E5D\u9C7C\u56FE\u300A\u5B09\u6C34\u300B</span>\n                                            </div>\n                                            <div class="clearfix question_consultation_item_center">\n                                                <div class="ask">\n                                                    \u95EE\n                                                </div>\n                                                <div class="ask_question">\n                                                    <strong>' + data[i].GoodsAsk.content + "</strong>\n                                                </div>\n                                                <div class=\"ask_time\">\n                                                    <span></span>\n                                                    <span>" + data[i].GoodsAsk.create_time.replace('.0', "") + "</span>\n                                                </div>\n                                            </div>\n                                        </li>";
                    }
                    $('.question_consultation_list').html(ask_list_html);
                }
            }
        });
    };

 
    //轮播图
    var mySwiper = new Swiper('.swiper-container', {
        slidesPerView: 5,
        loop: true,
        observer: true,
        observeParents: true,
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev'
        }
    });

    $('.swiper_box').mouseenter(function () {
        $('.btns').addClass('open');
    }).mouseleave(function () {
        $('.btns').removeClass('open');
    });
    // //规格的切换选择
    // $('.spec').click(function () {
    //     $('.payment_m').removeClass('open');
    //     $(this).find('.payment_m').addClass('open');
    // });
    var params = {
        "Goods": {
            "id": goods_id,
            "@column": "id,cat_id"
        }
    };
    send_ajax('post', '/get', params, function callback(data) {
        if (!data.Goods) {
            return;
        }
        var goods_list = JSON.parse(localStorage.getItem('goods_list'));
        if (goods_list) {
            goods_list.push(data.Goods);
        } else {
            var goods_list = [];
            goods_list.push(data.Goods);
        }

        var goods_list = deteleObject(goods_list);
        localStorage.setItem('goods_list', JSON.stringify(goods_list));
    });
    function deteleObject(obj) {
        var uniques = [];
        var stringify = {};
        for (var i = 0; i < obj.length; i++) {
            var keys = Object.keys(obj[i]);
            keys.sort(function (a, b) {
                return Number(a) - Number(b);
            });
            var str = '';
            for (var j = 0; j < keys.length; j++) {
                str += JSON.stringify(keys[j]);
                str += JSON.stringify(obj[i][keys[j]]);
            }
            if (!stringify.hasOwnProperty(str)) {
                uniques.push(obj[i]);
                stringify[str] = true;
            }
        }
        uniques = uniques;
        return uniques;
    };
    var clickparams = {
        "Goods": {
            "id": goods_id,
            "clicks+": 1
        }
    };
    //商品点击次数
    send_ajax('post', '/put', clickparams, function callback(res) {});

    //点击搜索跳转搜索页
    $('#search_btn').click(function () {
        var keyword = $('#search').val();
        if (keyword != '') {
            window.location.href = '/goods_search.html?keyword=' + keyword;
        }
    });
    $('.good_info a').click(function () {});
});